<?php

namespace App\Server\Util;

use GuzzleHttp\Pool;
use GuzzleHttp\Client;

class GuzzleRequest
{
    private static $queryKeys = [
        'GET' => 'query',
        'POST' => 'form_params',
    ];

    private static function clientInstance()
    {
        return new Client([
            // 'base_uri' => '',
        ]);
    }

    public static function normal($url, $params = null, $method = 'GET', $token = "")
    {
        $client =  self::clientInstance();
        $response = $client->request($method, $url, [
            'headers' => ['Authorization' => 'Bearer ' . $token],
            self::$queryKeys[$method] => $params ?: [],
        ]);
        $body = $response->getBody();
        $ret = $body->getContents();
        return json_decode($ret, true);
    }

    public static function raw($url, $params=[], $method="POST")
    {
        $client =  self::clientInstance();
        $response = $client->request(
            $method, 
            $url,
            [ 'body' => json_encode($params),]
        );
        $body = $response->getBody();
        $ret = $body->getContents();
        return $ret;
    }
}
